#!/usr/bin/env -S bash -e
#
# Backup databases with timestamp.
#
# @package    Bash
# @author     Kai Kimera <mail@kai.kim>
# @copyright  2023 iHub TO
# @license    MIT
# @version    0.0.1
# @link       https://lib.onl
# -------------------------------------------------------------------------------------------------------------------- #

(( EUID == 0 )) && { echo >&2 'This script should not be run as root!'; exit 1; }

# -------------------------------------------------------------------------------------------------------------------- #
# CONFIGURATION.
# -------------------------------------------------------------------------------------------------------------------- #

mysqldump="$( command -v mysqldump )"
p7zip="$( command -v 7zzs )"
date="$( command -v date )"
rm="$( command -v rm )"

# Help.
read -r -d '' help <<- EOF
Options:
  -u 'DB_USER'                          MySQL user name.
  -p 'DB_PASSWORD'                      MySQL user password.
  -d 'DB_1;DB_2;DB_3'                   MySQL databases (array).
  -s 'SECRET'                           Archive password.
EOF

# -------------------------------------------------------------------------------------------------------------------- #
# OPTIONS.
# -------------------------------------------------------------------------------------------------------------------- #

OPTIND=1

while getopts 'u:p:d:s:h' opt; do
  case ${opt} in
    u)
      user="${OPTARG}"
      ;;
    p)
      password="${OPTARG}"
      ;;
    d)
      dbs="${OPTARG}"; IFS=';' read -ra dbs <<< "${dbs}"
      ;;
    s)
      secret="${OPTARG}"
      ;;
    h|*)
      echo "${help}"; exit 2
      ;;
  esac
done

shift $(( OPTIND - 1 ))

(( ! ${#dbs[@]} )) && { echo >&2 '[ERROR] Databases not specified!'; exit 1; }

# -------------------------------------------------------------------------------------------------------------------- #
# INITIALIZATION.
# -------------------------------------------------------------------------------------------------------------------- #

init() {
  backup
}

# -------------------------------------------------------------------------------------------------------------------- #
# BACKUP: DATABASES.
# -------------------------------------------------------------------------------------------------------------------- #

backup() {
  ts="$( _timestamp )"

  for db in "${dbs[@]}"; do
    local name="${db}.${ts}.sql"

    echo '' && echo "--- OPEN: '${db}'"
    ${mysqldump} -u "${user}" -p"${password}" --single-transaction "${db}" > "${name}" \
      && ${p7zip} a -t7z -m0=lzma2 -mx=9 "${name}.7z" -p"${secret}" -mhe "${name}" \
      && ${rm} -f "${name}"
    echo '' && echo "--- DONE: '${db}'" && echo ''
  done
}

# -------------------------------------------------------------------------------------------------------------------- #
# ------------------------------------------------< COMMON FUNCTIONS >------------------------------------------------ #
# -------------------------------------------------------------------------------------------------------------------- #

# Timestamp: Date.
_timestamp() {
  ${date} -u '+%Y-%m-%d.%H-%M-%S'
}

# -------------------------------------------------------------------------------------------------------------------- #
# -------------------------------------------------< INIT FUNCTIONS >------------------------------------------------- #
# -------------------------------------------------------------------------------------------------------------------- #

init "$@"; exit 0
